﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Common;
using NP.BPMReportPlatform.FlowManagement.Dto;
using NP.BPMReportPlatform.RecurringJobs;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.FlowManagement
{
    /// <summary>
    /// 流程管理接口
    /// </summary>   
    [ApiController]
    [Route("api/FlowManagement")]
    public class FlowManagementController : BPMReportPlatformController
    {
        private readonly IFlowManagementAppService _flowManagementApp;
        private readonly IFlowManagerRecurringJob _flowManagerRecurring;
        private readonly ICommonService _commonService;

        public FlowManagementController(IFlowManagementAppService flowManagementApp, IFlowManagerRecurringJob flowManagerRecurring,
            ICommonService commonService)
        {
            _flowManagementApp = flowManagementApp;
            _flowManagerRecurring = flowManagerRecurring;
            _commonService = commonService;
        }

        ///// <summary>
        ///// 获取流程分类
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[Route("GetFlowCategory")]
        //public async Task<List<GetFlowCategoryOutput>> GetFlowCategory([FromQuery] GetFlowCategoryInput input)
        //{
        //    return await _flowManagementApp.GetFlowCategory(input);
        //}

        /// <summary>
        /// 获取流程分类
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetFlowCategoryNew")]
        public async Task<List<GetFlowCategoryOutput>> GetFlowCategoryNew([FromQuery] GetFlowCategoryInput input)
        {
            return await _flowManagementApp.GetFlowCategoryNew(input);
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("GetAllAsync")]
        public async Task<PagedResultDto<GetFlowOutput>> GetAllAsync([FromBody] GetFlowInput input)
        {
            return await _flowManagementApp.GetAllAsync(input);
        }


        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("ExportAsync")]
        public async Task<XlsxFileResult<GetFlowOutput>> ExportAllAsync([FromBody] GetFlowInput input)
        {
            var rst = await _flowManagementApp.ExportAllAsync(input);
            return new XlsxFileResult<GetFlowOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 查询框架订单数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("GetKJDDAllAsync")]
        public async Task<PagedResultDto<GetFlowOutput>> GetKJDDAllAsync([FromBody] GetFlowInput input)
        {
            //var workNumbers = _commonService.GetUserByRoleFilter(CurrentUser.UserName, Contract.Dtos.RelationshipTypeEnum.HRBP);
            //bool isAdmin = workNumbers.Contains("9999999999");
            //input.HRBPPermissionIsAdmin = isAdmin;
            //workNumbers = isAdmin ? new List<string>() : workNumbers;
            input.HRBPPermissionIsAdmin = true; // 暂时不用控制HRBP权限，有此页面的权限的都是薪资组，财务和IT

            input.IsHRBPPermission = true;
            return await _flowManagementApp.GetKJDDAllAsync(input, new List<string>());
        }

        /// <summary>
        /// 导出框架订单数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("ExportKJDDAllAsync")]
        public async Task<XlsxFileResult<GetFlowOutput>> ExportKJDDAllAsync([FromBody] GetFlowInput input)
        {
            var rst = await _flowManagementApp.ExportKJDDAllAsync(input);
            return new XlsxFileResult<GetFlowOutput>(rst, "导出Excel");
        }


        /// <summary>
        /// 框架订单作废
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("FrameOrderCancelAsync")]
        public async Task FrameOrderCancelAsync([FromBody] FrameOrderCancelInput input)
        {
            await _flowManagementApp.FrameOrderCancelAsync(input);
        }


        /// <summary>
        /// 车辆交通费报表作废需传true
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("FrameOrderCancelNoSAPAsync")]
        public async Task FrameOrderCancelNoSAPAsync([FromBody] FrameOrderCancelInput input)
        {
            await _flowManagementApp.FrameOrderCancelNoSAPAsync(input);
        }

        /// <summary>
        /// 流程作废
        /// </summary>
        /// <param name="instanceNumber"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FlowCancelAsync")]
        public async Task FlowCancelAsync([FromBody] FlowCancelInput input)
        {
            await _flowManagementApp.FlowCancelAsync(input);
        }

        /// <summary>
        /// 流程催办
        /// </summary>
        /// <param name="instanceNumber"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FlowUrgingAsync")]
        public async Task FlowUrgingAsync([FromBody] FlowUrgingInput input)
        {
            await _flowManagementApp.FlowUrgingAsync(input);
        }

        [HttpPost]
        [Route("AutoReplenishFlowTypeAsync")]
        public async Task AutoReplenishFlowTypeAsync()
        {
            await _flowManagerRecurring.AutoReplenishFlowTypeAsync();
        }

        /// <summary>
        /// 获取新BPM待办
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetBPMNEWToDoAsync")]
        public async Task<List<GetBPMNEWToDoOutput>> GetBPMNEWToDoAsync()
        {
            return await _flowManagementApp.GetBPMNEWToDoAsync();
        }

        /// <summary>
        /// 依据FlowCancelTask表数据 批量作废流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("FlowBatchCancelAsync")]
        public async Task FlowBatchCancelAsync([FromBody] FlowBatchCancelInput input)
        {
            await _flowManagementApp.FlowBatchCancelAsync(input);
        }

        /// <summary>
        /// 获取流程实例状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetProcessInstanceStatus")]
        public async Task<List<GetProcessInstanceStatusOutput>> GetProcessInstanceStatus([FromBody] List<GetProcessInstanceStatusInput> input)
        {
            if (input == null || input.Count == 0)
            {
                return new List<GetProcessInstanceStatusOutput>();
            }
            if (input.Count > 100)
            {
                throw new Exception("单次不能超过100个单据！");
            }

            return await _flowManagementApp.GetProcessInstanceStatus(input);
        }

        /// <summary>
        /// 获取BPM已办
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetBPMDoneAsync")]
        public async Task<List<GetBPMDoneOutput>> GetBPMDoneAsync([FromQuery] GetBPMDoneInput input)
        {
            return await _flowManagementApp.GetBPMDoneAsync(input);
        }
    }
}
